import React, { useEffect, useState } from 'react';
import { Button, Checkbox, Input, Radio, Select, notification, Form } from 'antd';
import '../../../../styles/Gml/xzbc.scss'
import { useLocation, useNavigate } from 'react-router-dom';
import * as qq from '../../../../api/Gml/agm'
interface P{}
function XzBc(props:P) {



    const [value, setValue] = useState('');
    const { state: row } = useLocation()
    var navigate = useNavigate()

    const onFinish = async (values: any) => {
        console.log('Success:', values);
        if (!row) {  //添加
            let res = await qq.bcAdd(values)
            // console.log(res);
            if (res.statusText == 'Created') {
              
                navigate('/index/attendance/attendance_manage/workforce_manage')
                notification.success({ message: '添加成功！' })
            }
        }else {   //修改           
            let res = await qq.bcUpdate(
                {
                    id:row.id,
                    ...values
                }
            )
            if(res.statusText=='OK'){
                notification.success({ message: '修改成功！' })
                navigate('/index/attendance/attendance_manage/workforce_manage')
            }
            
        }

    };

//回显表单数据
    useEffect(
        () => {
            console.log(row);
            if (row) {
                form.setFieldsValue(row)
                setValue(row.type)//单选框回显
            } else {
                form.setFieldsValue('')
            }

        }, []
    )


    const [form] = Form.useForm();

    return (
        <div className='bc'>
            <h2 className='h2'>新增班次</h2>
            <Form
                form={form}
                name="basic"
                labelCol={{ span: 8 }}
                wrapperCol={{ span: 16 }}
                style={{ maxWidth: 600 }}
                initialValues={{ remember: true }}
                onFinish={onFinish}
                autoComplete="off"
            >
             
                <Form.Item
                    label="班次"
                    name="type"
                    rules={[{ required: true, message: 'Please input your type' }]}
                >
                    <Radio.Group name="radiogroup" value={value}>
                        <Radio value={'固定班制'}>固定班制</Radio>
                        <Radio value={'排班制'}>弹性班次</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="班次名称"
                    name="name"
                    rules={[{ required: true, message: 'Please input your name!' }]}
                >
                    <Input placeholder='请输入'/>
                </Form.Item>
                   
                <Form.Item
                    label="上班时间"
                    name="time"
                    // rules={[{ required: true }]}
                >
                    <Select
                        defaultValue="9:00-18:00"
                        style={{ width: 120 }}
                        options={[
                            { value: '9:00-18:00', label: '9:00-18:00' },
                            { value: '9:00-17:00', label: '9:00-17:00' },
                            { value: '10:00-17:00', label: '10:00-17:00' },
                        ]}
                    />
                </Form.Item>

                <Form.Item
                    label="上班时长"
                    name="alltime"
                    // rules={[{ required: true}]}
                >
                    <Select
                        defaultValue="9小时"
                        style={{ width: 120 }}
                        options={[
                            { value: '8小时', label: '8小时' },
                            { value: '9小时', label: '9小时' },
                            { value: '7小时', label: '7小时' },
                        ]}
                    />
                </Form.Item>

                <Form.Item
                    label="操作人"
                    name="people"
                    rules={[{ required: true, message: 'Please input your num' }]}
                >
                    <Select
                        defaultValue=""
                        style={{ width: 120 }}
                        options={[
                            { value: '昕昕', label: '昕昕' },
                            { value: '嗨嗨', label: '嗨嗨' },
                            { value: '冬冬', label: '冬冬' },
                            { value: '莉莉', label: '莉莉' },
                        ]}
                    />
                </Form.Item>

                <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                    <Button type="primary" htmlType="submit">
                        添加
                    </Button>
                    <Button  htmlType="submit">
                        取消
                    </Button>
                </Form.Item>
            </Form>
           
        </div>
    );
}

export default XzBc;